<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="600"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-form-item label="密码" field="password">
        <a-input v-model="formData.password" placeholder="请输入密码" />
      </a-form-item>
      <a-form-item label="用户类型:" field="user_type">
        <a-input v-model="formData.user_type" placeholder="请输入用户类型:" />
      </a-form-item>
      <a-form-item label="用户昵称" field="nickname">
        <a-input v-model="formData.nickname" placeholder="请输入用户昵称" />
      </a-form-item>
      <a-form-item label="手机" field="phone">
        <a-input v-model="formData.phone" placeholder="请输入手机" />
      </a-form-item>
      <a-form-item label="用户邮箱" field="email">
        <a-input v-model="formData.email" placeholder="请输入用户邮箱" />
      </a-form-item>
      <a-form-item label="用户头像" field="avatar">
        <a-input v-model="formData.avatar" placeholder="请输入用户头像" />
      </a-form-item>
      <a-form-item label="个人签名" field="signed">
        <a-input v-model="formData.signed" placeholder="请输入个人签名" />
      </a-form-item>
      <a-form-item label="后台首页类型" field="dashboard">
        <a-input v-model="formData.dashboard" placeholder="请输入后台首页类型" />
      </a-form-item>
      <a-form-item label="状态" field="status">
        <a-input v-model="formData.status" placeholder="请输入状态" />
      </a-form-item>
      <a-form-item label="最后登陆IP" field="login_ip">
        <a-input v-model="formData.login_ip" placeholder="请输入最后登陆IP" />
      </a-form-item>
      <a-form-item label="最后登陆时间" field="login_time">
        <a-date-picker v-model="formData.login_time" :show-time="true" mode="date" placeholder="请选择最后登陆时间" />
      </a-form-item>
      <a-form-item label="后台设置数据" field="backend_setting">
        <a-input v-model="formData.backend_setting" placeholder="请输入后台设置数据" />
      </a-form-item>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import api from '../api/users'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')

let title = computed(() => {
  return '用户信息4' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  id: null,
  password: '',
  user_type: '100',
  nickname: '',
  phone: '',
  email: '',
  avatar: '',
  signed: '',
  dashboard: '',
  status: 1,
  login_ip: '',
  login_time: '',
  backend_setting: '',
})

// 验证规则
const rules = {
  username: [{ required: true, message: '用户名必需填写' }],
  password: [{ required: true, message: '密码必需填写' }],
  user_type: [{ required: true, message: '用户类型:必需填写' }],
  nickname: [{ required: true, message: '用户昵称必需填写' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
